package com.sengled.cloud.biz.union.mgt.dao.sys.provider;

import com.sengled.cloud.biz.union.mgt.model.po.Role;
import com.sengled.cloud.biz.union.mgt.model.vo.ShopQueryParamsBean;

import org.apache.commons.lang.StringUtils;

import java.text.MessageFormat;
import java.util.List;

/**
 * Created by csolo on 2017/11/8.
 */
public class ShopDaoProvider {

   

    public String queryShopSQL(ShopQueryParamsBean shopQueryParamsBean) {
        StringBuilder sb = new StringBuilder();
        sb.append("SELECT id,shop_code shopCode,shop_name shopName,shop_tel shopTel,CASE WHEN shop_nature=1 THEN '直营' WHEN shop_nature=2 THEN '加盟' ELSE '其他' END  shopNature FROM shop where seal_mark = 0 and int001 is null ");
        StringBuilder setStr = new StringBuilder();
   
        if(StringUtils.isNotEmpty(shopQueryParamsBean.getShopCode())){
            setStr.append(" and shop_code like CONCAT('%', #{shopCode}, '%') ");
        }
        if(StringUtils.isNotEmpty(shopQueryParamsBean.getShopName())){
            setStr.append(" and shop_name like CONCAT('%', #{shopName}, '%') ");
        }
        if(shopQueryParamsBean.getShopNature() != null && shopQueryParamsBean.getShopNature() != 0){
            setStr.append(" and shop_nature = #{shopNature}");
        }
        if(shopQueryParamsBean.getCompanyId() != null){
            setStr.append(" and company_id = #{companyId}");
        }
        if(!shopQueryParamsBean.isSuperMgr()&&shopQueryParamsBean.getUserId() != null){
            setStr.append(" and id in(SELECT sa.shop_id FROM user_acct_shop_acct usa,shop_account sa WHERE usa.user_account_id=#{userId} and usa.shop_account_id=sa.id and sa.seal_mark=0)");
        }
        
        setStr.append(" order by create_date desc" );
        
        if(shopQueryParamsBean.getStart()!=null&&shopQueryParamsBean.getPageSize()!=null){
        	setStr.append(" LIMIT #{start},#{pageSize}" );
        }
        
       
        sb.append(setStr);
        System.out.println("sql : ---------------->"+sb.toString());
        return sb.toString();
    }
    
    public String queryShopTotalRecordsSQL(ShopQueryParamsBean shopQueryParamsBean) {
        StringBuilder sb = new StringBuilder();
        sb.append("SELECT count(id) FROM shop where seal_mark = 0 and int001 is null ");
        StringBuilder setStr = new StringBuilder();
   
        if(StringUtils.isNotEmpty(shopQueryParamsBean.getShopCode())){
            setStr.append(" and shop_code like CONCAT('%', #{shopCode}, '%') ");
        }
        if(StringUtils.isNotEmpty(shopQueryParamsBean.getShopName())){
            setStr.append(" and shop_name like CONCAT('%', #{shopName}, '%') ");
        }
        if(shopQueryParamsBean.getShopNature() != null && shopQueryParamsBean.getShopNature() != 0){
            setStr.append(" and shop_nature = #{shopNature}");
        }
        if(shopQueryParamsBean.getCompanyId() != null){
            setStr.append(" and company_id = #{companyId}");
        }
        if(!shopQueryParamsBean.isSuperMgr()&&shopQueryParamsBean.getUserId() != null){
            setStr.append(" and id in(SELECT sa.shop_id FROM user_acct_shop_acct usa,shop_account sa WHERE usa.user_account_id=#{userId} and usa.shop_account_id=sa.id and sa.seal_mark=0)");
        }
        
        setStr.append("  order by create_date desc" );
        
        sb.append(setStr);
        System.out.println("sql : ---------------->"+sb.toString());
        return sb.toString();
    }
    

}
